package com.cn.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;



import com.cn.entity.Order_xiangmu;
import com.cn.entity.Orders;

public class OrderXiangmuDao {
	private static SessionFactory sessionFactory;
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	Session session;
	
	
	
	
	public List<Order_xiangmu> scanOrder(int orderid){
		
		session=sessionFactory.openSession();
		
		String sql="select      order_xian0_.id as id1_6_,  order_xian0_.orderid as orderid1_6_,        order_xian0_.xiangmuid as xiangmui2_6_,        order_xian0_.result as result3_6_     from        Order_xiangmu order_xian0_     where        order_xian0_.orderid=?";
		
		SQLQuery sq=session.createSQLQuery(sql);
		sq.setInteger(0, orderid);
		List<Object> list=sq.list();
		 List<Order_xiangmu> l=new ArrayList<Order_xiangmu>();
		for(int i=0;i<list.size();i++){
			Order_xiangmu or=new Order_xiangmu();
			Object[] o=(Object[])list.get(i);
			or.setId(Integer.parseInt(o[0].toString()));
			
			or.setOrderid(Integer.parseInt(o[1].toString()));
			
			or.setXiangmuid(Integer.parseInt(o[2].toString()));
			
			if (o[3]!=null){
			
			or.setResult(o[3].toString());}
			
			l.add(or);
			
	 
		}
		for(int i=0;i<l.size();i++){
			Order_xiangmu or=l.get(i);
			 System.out.println(or.getResult()+"11  "+  or.getXiangmuid());
		}
		return l;
		
	}
		
  
	public void add(Order_xiangmu ox){
		session=sessionFactory.openSession();
		Transaction tr=session.beginTransaction();
		
		session.save(ox);
		tr.commit();
		session.close();
		
		
		
	}
	
	
	public void update(Order_xiangmu ox){
		session=sessionFactory.openSession();
		Transaction tr=session.beginTransaction();
		
		session.update(ox);
		tr.commit();
		session.close();
		
		
		
	}
//	public static void main(String[] args) {
//		
//		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//		sessionFactory=(SessionFactory)ctx.getBean("sessionFactory");
//		OrderXiangmuDao d=new OrderXiangmuDao();
//		d.session=sessionFactory.openSession();
//		//String hql="from Order_xiangmu where orderid=1 ";
//		//Query query=session.createQuery(hql);
//		//query.setInteger(0, orderid);
//		
//		String sql="select        order_xian0_.orderid as orderid1_6_,        order_xian0_.xiangmuid as xiangmui2_6_,        order_xian0_.result as result3_6_     from        Order_xiangmu order_xian0_     where        order_xian0_.orderid=1";
//	
//		SQLQuery sq=d.session.createSQLQuery(sql);
//		List<Object> list=sq.list();
//		 List<Order_xiangmu> l=new ArrayList<Order_xiangmu>();
//		for(int i=0;i<list.size();i++){
//			Order_xiangmu or=new Order_xiangmu();
//			Object[] o=(Object[])list.get(i);
//			or.setOrderid(Integer.parseInt(o[0].toString()));
//			or.setXiangmuid(Integer.parseInt(o[1].toString()));
//			or.setResult(o[2].toString());
//			l.add(or);
//			
//	 
//		}
//		for(int i=0;i<l.size();i++){
//			Order_xiangmu or=l.get(i);
//			 System.out.println(or.getResult()+"11  "+  or.getXiangmuid());
//		}
//		
//	}
}
